package util.boyaa 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	/**
	 * 元件居中
	 * @author Fly2011
	 */
	public class MCCenter 
	{
		
		static public function to_Center(arr:Array, cont:DisplayObjectContainer, gap:int):Sprite 
		{
			var s:Sprite = new Sprite();
			for (var i:int = 0; i < arr.length; i++) 
			{
				var mc:DisplayObject = arr[i];
				if (i != 0)
					mc.x = i * gap;
				s.addChild(mc);
			}
			cont && cont.addChild(s);
			var rect:Rectangle = s.getBounds(s.parent);
			s.x -=  (rect.x + rect.width / 2);
			//s.y -=  (rect.y + rect.height / 2);
			return s;
		}
		
		
		static public function to_Center_Morerow(arr:Array,cont:DisplayObjectContainer,gap:int,column:int,height:int):void
		{
			var s:Sprite = new Sprite();
			var index_c:int = 0;
			var index_r:int = 0;
			for (var i:int = 0; i < arr.length; i++) 
			{
				var mc:DisplayObject = arr[i];
				mc.x = index_c * gap;
				mc.y = index_r * height;
				index_c++;	
				if (index_c == column )
				{
					index_c = 0;
					index_r ++;
				}
				s.addChild(mc);
			}
			cont.addChild(s);
			var rect:Rectangle = s.getBounds(s.parent);
			s.x -=  (rect.x + rect.width / 2);
		}
		
		/**
		 * 竖直排列显示对象
		 * @param	arr
		 * @param	gap
		 * @return
		 */
		static public function arrangedVertically(arr:Array, gap:int = 10):Sprite
		{
			var s:Sprite = new Sprite();
			for (var i:int = 0; i < arr.length; i++)
			{
				var mc:DisplayObject = arr[i];
				if (i != 0) mc.y = s.getBounds(s).height + gap;
				s.addChild(mc);
			}
			return s;
		}
		
	}

}